package article

import (
	"strconv"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Core"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Rt"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/App/admin"
)

type Admin struct {

}

func (_ Admin) Setup() {
	Core.SetRoute("GET:/admin/articles/add", "[I]article.Admin.AddAction")
	Core.SetRoute("POST:/admin/articles/add", "[I]article.Admin.PostAction")
	Core.SetRoute("GET:/admin/articles/edit", "[I]article.Admin.EditAction")
	Core.SetRoute("POST:/admin/articles/edit", "[I]article.Admin.EditPostAction")
	Core.SetRoute("DELETE:/admin/articles/delete", "[I]article.Admin.DeleteAction")
	Core.SetRoute("GET:/admin/articles", "[I]article.Admin.ListAction")
	Core.SetRoute("GET:/admin/articles/category", "[I]article.Admin.ListCategoryedAction")
	Core.SetTemplateFunc("GetArticleStatusText", StatusText)
	Core.SetTemplateFunc("DateFormatString", Rt.Time.FormatS)
}

// article page action
func (_ Admin) ListAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	page := 1
	if len(request.Param) >= 5 {
		page, _ = strconv.Atoi(request.Param[4])
		if page < 1 {
			page = 1
		}
	}
	data := make(map[string]interface {})
	data["Categories"] = categoryGetList()
	data["Articles"], data["Pager"] = adminList(page, 8, nil, "/admin/articles/")
	response.Body = admin.RenderAdminPage("article/list.html", data, nil, "article")
}

// article page action
func (_ Admin) ListCategoryedAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 6 {
		response.Redirect("/admin/articles/")
		return
	}
	// get current category
	category := categoryGetBySlug(request.Param[4])
	if len(category) < 1 {
		response.Redirect("/admin/articles/")
		return
	}
	page := 1
	if len(request.Param) >= 7 {
		page, _ = strconv.Atoi(request.Param[6])
		if page < 1 {
			page = 1
		}
	}
	data := make(map[string]interface {})
	data["Categories"] = categoryGetList()
	data["Category"] = category
	data["Articles"], data["Pager"] = adminList(page, 8, map[string]interface {}{
			"category":category["id"],
		}, "/admin/articles/category/" + category["slug"] + "/")
	response.Body = admin.RenderAdminPage("article/list.html", data, nil, "article")
}

// article edit page action
func (_ Admin) EditAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 6 {
		response.Redirect("/admin/articles/")
		return
	}
	id := request.Param[4]
	article := GetOneById(id)
	if len(article) < 1 {
		response.Redirect("/admin/articles/")
		return
	}
	data := make(map[string]interface {})
	data["Categories"] = categoryGetList()
	data["Article"] = article
	response.Body = admin.RenderAdminPage("article/edit.html", data, nil, "article")
}

// article add page action
func (_ Admin) AddAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	data := make(map[string]interface {})
	data["Categories"] = categoryGetList()
	response.Body = admin.RenderAdminPage("article/add.html", data, nil, "article")
}

// article add action
func (_ Admin) PostAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	data := request.Input().All()
	res := map[string]interface {}{"res":false}
	// check data validation
	msg := validateArticleData(data, "")
	if msg != "" {
		res["msg"] = msg
		response.Json(res)
		return
	}
	// assign author id
	data["author"] = request.Cookie("user_id")
	id := AddArticle(data)
	res["res"] = true
	res["id"] = id
	response.Json(res)
}

// article edit action
func (_ Admin) EditPostAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 6 {
		response.Status = 400
		return
	}
	id := request.Param[4]
	data := request.Input().All()
	res := map[string]interface {}{"res":false}
	msg := validateArticleData(data, id)
	if msg != "" {
		res["msg"] = msg
		response.Json(res)
		return
	}
	UpdateArticle(id, data)
	res["res"] = true
	response.Json(res)
}

// article deletion action
func (_ Admin) DeleteAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 6 {
		response.Status = 400
		return
	}
	id := request.Param[4]
	DeleteArticle(id)
	res := map[string]interface {}{"res":true}
	response.Json(res)
}
